package com.wu.creditmanager;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.wu.creditmanager.adapter.BillAdapter;
import com.wu.creditmanager.db.CreditsDbAdapter;
import com.wu.creditmanager.dialog.ActionItem;
import com.wu.creditmanager.dialog.BillFilterDialog;
import com.wu.creditmanager.dialog.BillFilterDialog.CallBack;
import com.wu.creditmanager.dialog.EditDialogBuilder;
import com.wu.creditmanager.dialog.EditDialogBuilder.OnInputValueListener;
import com.wu.creditmanager.dialog.QuickActions;
import com.wu.creditmanager.entity.Bill;
import com.wu.creditmanager.util.Constants;
import com.wu.creditmanager.util.LayoutUtil;
import com.wu.creditmanager.util.StringUtil;
import com.wu.creditmanager.util.TimeUtil;

public class BillList extends Activity implements OnItemClickListener {
	protected static final String FILTER = "filter";
	private ListView listview;
	private CreditsDbAdapter mDbHelper;
	private List<Bill> bills;
	private String filter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bill_list);
		listview = (ListView) findViewById(R.id.bill_list_id_list);
		mDbHelper = new CreditsDbAdapter(this);
		Bundle extras = getIntent().getExtras();
		if (extras == null || StringUtil.isEmpty(extras.getString(FILTER))) {
			this.filter = "bi.bill_month='"
					+ TimeUtil.getCurrentTime().format(
							Constants.BILL_MONTH_FORMAT) + "'";
			LayoutUtil.initToolbar(findViewById(R.id.tbar), getResources()
					.getString(R.string.title_bill_list), null, null,
					getResources().getString(R.string.btn_filter),
					btn2ClickListener);
		} else {
			this.filter = extras.getString(FILTER);
			LayoutUtil
					.initToolbar(findViewById(R.id.tbar), getResources()
							.getString(R.string.title_bill_list),
							getResources().getString(R.string.back),
							new OnClickListener() {
								public void onClick(View v) {
									finish();
								}
							}, getResources().getString(R.string.btn_filter),
							btn2ClickListener);
		}
		loadBills(filter);
		listview.setOnItemClickListener(this);

	}

	private OnClickListener btn2ClickListener = new OnClickListener() {
		public void onClick(View v) {
			showDialog(R.id.tbar_btn2);
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case R.id.tbar_btn2:
			return new BillFilterDialog(this, new CallBack() {
				public void filter(boolean paid, boolean notpaid,
						String creditId, String billMonth) {
					List<String> list = new ArrayList<String>();
					if (paid && notpaid) {

					} else if (paid) {
						list.add("bi.paid = 1");
					} else if (notpaid) {
						list.add("bi.paid = 0");
					}
					if (!"-1".equals(creditId)) {
						list.add("bi.credit_id=" + creditId);
					}
					if (!"-1".equals(billMonth)) {
						list.add("bi.bill_month='" + billMonth + "'");
					}
					filter = StringUtil.join(list, " and ");
					loadBills(filter);
				}
			});
		default:
			break;
		}
		return null;
	};

	private void loadBills(String filter) {
		mDbHelper.open();
		bills = mDbHelper.fetchAllBills(filter);
		mDbHelper.close();
		BillAdapter adapter = new BillAdapter(this, bills);
		adapter.setShowPayState(true);
		listview.setAdapter(adapter);
	}

	public void onItemClick(AdapterView<?> listview, View v, int position,
			long id) {
		final Bill bill = bills.get(position % bills.size());
		showQuickDialog(bill, v);
	}

	private void showQuickDialog(final Bill bill, View v) {
		QuickActions quickActions = new QuickActions(v);
		quickActions.addActionItem(new ActionItem(getResources().getDrawable(
				R.drawable.money), getResources().getString(
				R.string.update_amount_payable), new OnClickListener() {
			public void onClick(View v) {
				showUpdateAmountPayableDialog(bill);
			}
		}));
		if (!bill.isPaid()) {
			quickActions.addActionItem(new ActionItem(getResources()
					.getDrawable(R.drawable.money), getResources().getString(
					R.string.pay), new OnClickListener() {
				public void onClick(View v) {
					showPayDialog(bill);
				}
			}));
		} else {
			quickActions.addActionItem(new ActionItem(getResources()
					.getDrawable(R.drawable.money), getResources().getString(
					R.string.update_pay_amount), new OnClickListener() {
				public void onClick(View v) {
					showUpdatePayAmountDialog(bill);
				}
			}));
		}
		quickActions.show();
	}

	private void showUpdatePayAmountDialog(final Bill bill) {
		new EditDialogBuilder(this, R.string.entryPayAmount)
				.setInputType(
						InputType.TYPE_CLASS_NUMBER
								| InputType.TYPE_NUMBER_FLAG_SIGNED
								| InputType.TYPE_NUMBER_FLAG_DECIMAL)
				.setOnInputValueListener(new OnInputValueListener() {
					public void setValue(String value) {
						if (StringUtil.isEmpty(value)) {
							Toast.makeText(BillList.this, R.string.empty_entry,
									Toast.LENGTH_SHORT).show();
							return;
						}
						bill.setPayAmount(Double.valueOf(value));
						mDbHelper.open(true);
						mDbHelper.updateBill(bill);
						mDbHelper.close();
						loadBills(filter);
					}
				}).show();
	}

	private void showUpdateAmountPayableDialog(final Bill bill) {
		new EditDialogBuilder(this, R.string.entryAmountPayalbe)
				.setInputType(
						InputType.TYPE_CLASS_NUMBER
								| InputType.TYPE_NUMBER_FLAG_SIGNED
								| InputType.TYPE_NUMBER_FLAG_DECIMAL)
				.setOnInputValueListener(new OnInputValueListener() {
					public void setValue(String value) {
						if (StringUtil.isEmpty(value)) {
							Toast.makeText(BillList.this, R.string.empty_entry,
									Toast.LENGTH_SHORT).show();
							return;
						}
						bill.setAmountPayable(Double.valueOf(value));
						mDbHelper.open(true);
						mDbHelper.updateBill(bill);
						mDbHelper.close();
						loadBills(filter);
					}
				}).show();
	}

	private void showPayDialog(final Bill bill) {
		new EditDialogBuilder(this, R.string.entryPayAmount)
				.setInputType(
						InputType.TYPE_CLASS_NUMBER
								| InputType.TYPE_NUMBER_FLAG_SIGNED
								| InputType.TYPE_NUMBER_FLAG_DECIMAL)
				.setOnInputValueListener(new OnInputValueListener() {

					public void setValue(String value) {
						if (StringUtil.isEmpty(value)) {
							Toast.makeText(BillList.this, R.string.empty_entry,
									Toast.LENGTH_SHORT).show();
							return;
						}
						bill.setPaid(true);
						bill.setPaytime(System.currentTimeMillis());
						bill.setPayAmount(Double.valueOf(value));
						mDbHelper.open(true);
						mDbHelper.updateBill(bill);
						mDbHelper.close();
						loadBills(filter);
					}
				}).show();
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadBills(filter);
	}

}
